extends CharacterBody2D


@export var move_speed : float = 50
@export var animator : AnimatedSprite2D
@export var is_game_over : bool = false
#子弹场景
@export var bullet_scene : PackedScene
@export var audio : AudioStreamPlayer
@export var overSound : AudioStreamPlayer
@export var runningSound : AudioStreamPlayer

func _process(delta: float) -> void:
	if velocity == Vector2.ZERO or is_game_over:
		runningSound.stop()
	elif not runningSound.playing :
		runningSound.play()
		

func _physics_process(delta: float) -> void:
	#设置移动速度
	if not is_game_over:
		velocity = Input.get_vector("left","right","up","down") * move_speed
		if velocity == Vector2.ZERO:
			animator.play("idle")
		else:
			animator.play("run")
	#根据设置的velocity
		move_and_slide()

func game_over():
	if not is_game_over:
		is_game_over = true
		animator.play("game_over")
		overSound.play()
		get_tree().current_scene.show_game_over()
		$RestartTimeer.start()
		
	

func _on_fire() -> void:
	if velocity != Vector2.ZERO or is_game_over:
		return
	audio.play()
	var bullet_node = bullet_scene.instantiate()
	bullet_node.position = position + Vector2(6,6)
	get_tree().current_scene.add_child(bullet_node)


func _on_restart_timeer_timeout() -> void:
		get_tree().reload_current_scene()
